import {defineStore} from 'pinia';

type Tag = {
    id: string | number;
    name?: string;
    key: string;
    path: string;
    meta: {} | any;
    children: Tag[];
}
export const useTag = defineStore('tag', {
    state:():any=> {
        return {
            openTags:[],
            //当前活跃的
            currentlyActive:{} as any

        }
    },
    actions: {
        addTag(tag: Tag): void {            
            let isrepeat:boolean = false;
            for( let i:number = 0; i< this.openTags.length ; i++ ){
                if( this.openTags[i].id == tag.id ){
                    isrepeat = true
                    break;
                }
            }
            
            this.currentlyActive = tag
            if( this.openTags.length == 0 || !isrepeat){
                this.openTags.push( tag )
            }

        },
        removeTag(key:string):void{
            // console.log('@@***')
            for( let i:number = 0; i< this.openTags.length ; i++ ){
                if( this.openTags[i].key  == key ){
                    // console.log('@@',i)
                    this.openTags.splice(i,1)
                    break;
                }
            }
        }
    },
    getters: {
        getOpenTags(): Tag[] {
            return this.openTags;
        }
    }
})
